package com.weiyi.mashangzhu.Base;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

public abstract class BaseListAdapter<T> extends BaseAdapter {

    protected Context mContext;
    protected List<T> mList;
    private int maxSize = 0;

    public BaseListAdapter(Context context, List<T> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public int getCount() {

        if (mList != null && mList.size() > 0) {
            if (maxSize == 0) {
                return mList.size();
            } else {
                return mList.size() > maxSize ? maxSize : mList.size();
            }
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        if (mList != null && mList.size() > 0) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return initView(position, convertView, parent);
    }

    public abstract View initView(int position, View convertView, ViewGroup parent);

    public void setMaxSize(int size) {
        maxSize = size;
    }
}
